package model.impl;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import bean.UserBean;
import model.IUserLoginModel;
import model.OnLoginListener;
import utils.GetResponse;

/**
 * 具体类：用户登录业务逻辑
 */
public class UserLoginModel implements IUserLoginModel {

	@Override
	public void login(final String username, final String password,
			final OnLoginListener onLoginListener) {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				String url= "http://test.mingslife.com/AppServer/user_save.action?loginName="+username+"ming&password="+password;
				try {
					String rs = GetResponse.request(url);
					JSONObject root = new JSONObject(rs);
					String result = root.getString("result");
					if(result.equals("success")){
						UserBean user = new UserBean();
						user.setLoginName(username);
						user.setPassword(password);
						onLoginListener.onLoginSuccess(user);
					}else{
						onLoginListener.onLoginFailed(result);
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

}
